﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Aliyun.OpenServices.OpenStorageService;
using Top.Common;

namespace Top.Oss
{
    /// <summary>
    /// 客户OssClient提供类
    /// </summary>
    public sealed class OssClientProvider
    {
        public static OssConfig ossConfig
        {
            get;
            set;
        }

        private OssClientProvider()
        {
        }

        static OssClientProvider()
        {
            //初始化配置信息 可以从数据库、配置文件获取
            ossConfig = new OssConfig()
            {
                AccessId =TopConfigHelper.GetAppSettings("OssAccessId"),
                AccessKey = TopConfigHelper.GetAppSettings("OssAccessKey"),
                Endpoint = TopConfigHelper.GetAppSettings("OssEndpoint")
            };
        }

        private static OssClient ossClient;
        private static Object obj = new object();

        /// <summary>
        /// 获取访问阿里云开放存储服务（Open Storage Service， OSS）的入口类
        /// </summary>
        /// <returns></returns>
        public static OssClient GetOssClient()
        {
            if (ossClient == null)
            {
                lock (obj)
                {
                    if (ossClient == null)
                    {
                        ossClient = new OssClient(ossConfig.Endpoint, ossConfig.AccessId, ossConfig.AccessKey);
                    }
                }
            }

            return ossClient;
        }
    }
}
